package com.liang.jiekou.config;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfig {
    // 从application.yml文件中读取最大连接数
    @Value("${httpclient.maxTotal}")
    private int maxTotal;

    // 从application.yml文件中读取每个路由的默认最大连接数
    @Value("${httpclient.defaultMaxPerRoute}")
    private int defaultMaxPerRoute;

    @Bean
    public CloseableHttpClient httpClient() {
        // 创建连接管理器，并设置最大连接数和每个路由的默认最大连接数
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(maxTotal);
        cm.setDefaultMaxPerRoute(defaultMaxPerRoute);

        // 使用连接管理器创建HttpClient
        return HttpClients.custom()
                .setConnectionManager(cm)
                .build();
    }

}
